package org.scboy.gateway.filters;

import org.springframework.core.Ordered;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.apache.dubbo.config.annotation.DubboReference;
import org.scboy.dbms.api.AreaManager;
import org.scboy.dbms.api.DictionaryManager;
import org.scboy.gateway.decorator.ConvertDecorator;
import org.scboy.rbac.api.OrgManager;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.web.server.ServerWebExchange;

import cn.hutool.core.util.StrUtil;
import reactor.core.publisher.Mono;

/**
 * 转换数据拦截
 * @author 杨浩
 */
@Configuration
public class ConvertGlobalFilter implements GlobalFilter, Ordered {
	

	@DubboReference
	OrgManager org;

	@DubboReference
	AreaManager area;

	@DubboReference
	DictionaryManager dic;
	
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		
		//判断是否有转换请求头
		String convertConfig = exchange.getRequest().getHeaders().getFirst("Bmps-Convert-Config");
		
		//处理数据转换
		if(StrUtil.isNotEmpty(convertConfig)) {
			//返回值处理
			ConvertDecorator decorator = new ConvertDecorator(org,area,dic,exchange.getRequest(), exchange.getResponse());
			return chain.filter(exchange.mutate().response(decorator).build());
		}else {
			return chain.filter(exchange);
		}
	}

	@Override
	public int getOrder() {
		return -1000;
	}
}